﻿using ToolKitIESiteList.Library;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics.CodeAnalysis;

namespace ToolKitManagerIESiteList
{
    public partial class FrmSanitize : Form
    {
        private bool Resizing = false;

        public FrmSanitize()
        {
            InitializeComponent();
        }

        [SuppressMessage("Microsoft.Design", "CA1006")]
        [SuppressMessage("Microsoft.Design", "CA1002")]
        [SuppressMessage("Microsoft.Usage", "CA2227")]
        public List<Dictionary<string, List<IEModeDataItem>>> DataSanitaze { get; set; }

        [SuppressMessage("Microsoft.Performance", "CA1819")]
        public IEModeDataItem[] ResultSanitaze { get; private set; }

        private void FrmSanitize_Load(object sender, EventArgs e)
        {
            ResultSanitaze = new IEModeDataItem[] { };
            listViewDataSanitize.SuspendLayout();
            listViewDataSanitize.Items.Clear();
            listViewDataSanitize.Groups.Clear();
            foreach (var item in DataSanitaze)
            {
                foreach (var itemkey in item)
                {
                    var pos = listViewDataSanitize.Groups.Add(new ListViewGroup(itemkey.Key, HorizontalAlignment.Left));
                    foreach (var itemValue in itemkey.Value)
                    {
                        var key = itemValue.Url;
                        ListViewItem lvi = new ListViewItem(key);
                        lvi.Tag = itemValue;
                        lvi.Checked = true;
                        lvi.Group = listViewDataSanitize.Groups[pos];
                        lvi.SubItems.Add(itemValue.CompatMode.ToString());
                        lvi.SubItems.Add(itemValue.OpenIn.ToString());
                        listViewDataSanitize.Items.Add(lvi);
                    }
                }
            }
            ListView_SizeChanged(listViewDataSanitize, e);
        }


        private void ListView_SizeChanged(object sender, EventArgs e)
        {
            // Don't allow overlapping of SizeChanged calls
            if (!Resizing)
            {
                // Set the resizing flag
                Resizing = true;

                ListView listView = sender as ListView;
                if (listView != null)
                {
                    float totalColumnWidth = 0;

                    // Get the sum of all column tags
                    for (int i = 0; i < listView.Columns.Count; i++)
                        totalColumnWidth += Convert.ToInt32(listView.Columns[i].Tag);

                    // Calculate the percentage of space each column should 
                    // occupy in reference to the other columns and then set the 
                    // width of the column to that percentage of the visible space.
                    for (int i = 0; i < listView.Columns.Count; i++)
                    {
                        float colPercentage = (Convert.ToInt32(listView.Columns[i].Tag) / totalColumnWidth);
                        listView.Columns[i].Width = (int)(colPercentage * listView.ClientRectangle.Width);
                    }
                }
            }
            // Clear the resizing flag
            Resizing = false;
        }

        private void BntConfirm_Click(object sender, EventArgs e)
        {
            ResultSanitaze = listViewDataSanitize.CheckedItems.Cast<ListViewItem>()
                .Select(s => (IEModeDataItem)s.Tag)
                .ToArray();
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
